package com.nuoniu.sibanyun.controller.work;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nuoniu.sibanyun.common.BaseController;
import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.lang.StringUtils;
import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.entity.work.WorkReportUser;
import com.nuoniu.sibanyun.entity.work.WorkSummary;
import com.nuoniu.sibanyun.service.work.IWorkReportUserService;
import com.nuoniu.sibanyun.service.work.IWorkSummaryService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 工作总结 前端控制器
 * </p>
 *
 * @author baiqiang
 * @since 2020-09-15
 */
@RestController
@RequestMapping("/work/summary")
@Api(tags = "工作总结", value = "工作总结")
public class WorkSummaryController extends BaseController {
    
    @Autowired
    private IWorkSummaryService summaryService;
    @Autowired
    private IWorkReportUserService reportUserService;

    /**
     * 获取工作总结列表
     * @author 小啊刚
     * @date 2020-08-14
     */
    @GetMapping("pageList")
    public HttpResult pageList(Page page, WorkSummary workSummary) {
        return HttpResult.ok(summaryService.pageList(page, workSummary));
    }

    /**
     * 保存工作总结
     * @author 小啊刚
     * @date 2020-08-15
     */
    @PostMapping("save")
    public HttpResult saveWorkSummary(@RequestBody WorkSummary workSummary) throws ParseException {
        UserInfo userInfo = UserThreadLocal.get();
//        if(workSummary.getSubmitId() == null) {
//            throw new NuoNiuException("报送人不能为空");
//        }
//
//        if(workSummary.getSubmitId().equals(userInfo.getUserId())) {
//            throw new NuoNiuException("不能报送给自己");
//        }
//
//        if(StringUtils.isNotBlank(workSummary.getCcId())) {
//            String[] ids = workSummary.getCcId().split(",");
//            for(String id : ids) {
//                if(workSummary.getSubmitId().equals(Integer.parseInt(id))) {
//                    throw new NuoNiuException("报送人和抄送不能相同");
//                }
//            }
//        }

//        WorkReportUser reportUser = reportUserService.getDetail(userInfo.getOfficeId(), workSummary.getType(), 3);
//        if(workSummary.getId() != null) {
//            WorkSummary summary = summaryService.getById(workSummary.getId());
//            if(summary.getStatus() != 0) {
//                throw new NuoNiuException("已提交,无法修改");
//            }
//
//            if(summary.getStatus() == 0 && workSummary.getStatus() ==1) {
//                if(reportUser != null && StringUtils.isNotBlank(reportUser.getDeadline())) {
//                    workSummary.setCreateDate(summary.getCreateDate());
//                    Boolean flag = checkReport(workSummary, reportUser);
//                    if(!flag) {
//                        throw new NuoNiuException("提交失败,已超时");
//                    }
//                }
//                workSummary.setSubmitDate(LocalDateTime.now());
//            }
//            summaryService.updateById(workSummary);
//        }else {
//            workSummary.setCreateDate(new Date());
//            if(workSummary.getStatus() == 1 && reportUser != null && StringUtils.isNotBlank(reportUser.getDeadline()) && workSummary.getType() != 1) {
//                Boolean flag = checkReport(workSummary, reportUser);
//                if(!flag) {
//                    throw new NuoNiuException("提交失败,已超时");
//                }
//            }
//            if(workSummary.getStatus() != 0) {
//                workSummary.setSubmitDate(LocalDateTime.now());
//            }
//            workSummary.setCreateUserName(userInfo.getRealName());
//            workSummary.setCreateUserId(userInfo.getUserId());
//            workSummary.setCompanyId(userInfo.getCompanyId());
//            summaryService.save(workSummary);
//        }
        return HttpResult.ok();
    }

    /**
     * 删除工作总结
     * @author 小啊刚
     * @date 2020-08-15
     */
    @PostMapping("delete")
    public HttpResult delete(@RequestBody List<String> ids) {
        for(String id : ids) {
            WorkSummary workSummary = summaryService.getById(id);
            if(workSummary.getStatus() != 0) {
                throw new NuoNiuException("已报送，不能删除");
            }
            summaryService.removeById(id);
        }
        return HttpResult.ok();
    }

    /**
     * 报送总结
     * @author 小啊刚
     * @date 2020-08-17
     */
    @PostMapping("submitWorkSummary")
    public HttpResult submitWorkSummary(@RequestBody WorkSummary workSummary) throws ParseException {
//        UserInfo userInfo = UserThreadLocal.get();
//        if(workSummary.getSubmitId() == null) {
//            throw new NuoNiuException("报送人不能为空");
//        }
//        if(StringUtils.isNotBlank(workSummary.getCcId())) {
//            String[] ids = workSummary.getCcId().split(",");
//            for(String id : ids) {
//                if(workSummary.getSubmitId().equals(id)) {
//                    throw new NuoNiuException("报送人和抄送不能相同");
//                }
//            }
//        }
//        if(workSummary.getStatus() != 0) {
//            throw new NuoNiuException("请选择未报送的总结");
//        }
//        if(workSummary.getSubmitId().equals(userInfo.getUserId())) {
//            throw new NuoNiuException("不能报送给自己");
//        }
//
//        WorkReportUser reportUser = reportUserService.getDetail(userInfo.getOfficeId(), workSummary.getType(), 3);
//        WorkSummary form = summaryService.getById(workSummary.getId());
//        workSummary.setCreateDate(form.getCreateDate());
//        if(reportUser != null && StringUtils.isNotBlank(reportUser.getDeadline())) {
//            Boolean flag = checkReport(workSummary, reportUser);
//            if(!flag) {
//                throw new NuoNiuException("提交失败,已超时");
//            }
//        }
//
//        workSummary.setStatus(1);
//        workSummary.setSubmitDate(LocalDateTime.now());
//        summaryService.updateById(workSummary);
        return HttpResult.ok();
    }

    /**
     * 评阅总结
     * @author 小啊刚
     * @date 2020-08-17
     */
    @ApiOperation("评阅总结")
    @PostMapping("readWorkSummary")
    public HttpResult readWorkSummary(@RequestBody WorkSummary workSummary) {
        UserInfo userInfo = UserThreadLocal.get();
        WorkSummary form = summaryService.getById(workSummary.getId());
        if(!userInfo.getUserId().equals(form.getSubmitId())) {
            throw new NuoNiuException("被报送人才能评价");
        }
        if(StringUtils.isNotBlank(form.getEvaluationLevel()) && StringUtils.isNotBlank(form.getEvaluationContent())) {
            throw new NuoNiuException("已评价,不能再次评价");
        }
        workSummary.setReadStatus(1);
        summaryService.updateById(workSummary);
        return HttpResult.ok();
    }

}
